const App = getApp();
import {
    url
} from '../../../../../modelApi/url_config.js'
Page({
    data: {
        orderlistTop: '',
        statusBarHeight: "",
        statusBarPadding: "",
        nonelistFlag: false,
        menu: [{
            name: "全部",
            selectStatus: 0,
            orderStatus: ''
        }, {
            name: "待付款",
            selectStatus: 1,
            orderStatus: '0'
        }, {
            name: "待发货",
            selectStatus: 2,
            orderStatus: '1'
        }, {
            name: "待收货",
            selectStatus: 3,
            orderStatus: '2'
        }, {
            name: "已完成",
            selectStatus: 4,
            orderStatus: '9'
        }, {
            name: "已取消",
            selectStatus: 5,
            orderStatus: '7'
        }, {
            name: "已关闭",
            selectStatus: 6,
            orderStatus: '8'
        }],
        pageObj: {
            pageNum: 1,
            pageSize: 10
        },
        hasNextPage: false,
        orderListStatus: '',
        selectedTab: null, // 选tab
        screentabWidth: wx.getSystemInfoSync().windowWidth * 0.22,
        scrollWidthpx: 0,
        orderList: [],
    },
    onLoad: function (options) {
        const orderstatus = options.orderstatus;
        let index = this.data.menu.findIndex(function (el) {
            return el.orderStatus === orderstatus;
        });
        const selectindex = index <= 1 ? 0 : index - 1;
        this.setData({
            selectedTab: index != -1 ? index : 0,
            statusBarPadding: App.globalData.navTop,
            statusBarHeight: App.globalData.navHeight,
            orderlistTop: 45 + 6,
            orderListStatus: orderstatus,
            scrollWidthpx: selectindex * this.data.screentabWidth,
        });
        this.loadorderList(orderstatus);
    },
    selectMenu: function (e) {
        var selectStatus = e.target.dataset.selectStatus,
            selectIndex = e.target.dataset.index,
            orderStatus = e.target.dataset.orderStatus
        const index = selectIndex <= 1 ? 0 : selectIndex - 1
        this.setData({
            selectedTab: selectIndex,
            scrollWidthpx: index * this.data.screentabWidth,
            orderList: [],
            hasNextPage: false,
            nonelistFlag: false,
            orderListStatus: orderStatus
        });
        this.loadorderList(orderStatus);
    },
    bindscrolltolower() {
        if (this.data.hasNextPage) {
            this.data.pageObj.pageNum++;
            this.setData({
                pageObj: this.data.pageObj
            })
            this.loadorderList()
        }
    },
    /* 获取订单列表 */
    loadorderList(orderstatus) {
        const params = {
            status: orderstatus || this.data.orderListStatus,
            ...this.data.pageObj
        }
        App.ajax(url.orderListuRL, params, 'GET').then(res => {
            const arrobj = {
                '0': '待付款',
                '1': '待发货',
                '2': '待收货',
                '3': '待评价',
                '4': '已取消',
                '9': '已完成',
            }
            const rows = res.data.rows;
            rows.length && rows.forEach(el => {
                el.orderstatusText = arrobj[el.orderStatus];
            })
            this.setData({
                orderList: [...this.data.orderList, ...rows],
                hasNextPage: res.data.hasNextPage,
                nonelistFlag: true,
            })
        })
    },
    toorderDetial(e) {
        wx.navigateTo({
            url: `/pages/orderDetail/index?orderId=${e.currentTarget.dataset.id}`,
        })
    },
})